package com.itheima;

import sun.management.resources.agent;
import sun.misc.BASE64Encoder;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@WebServlet("/xiazai2")
public class xiazai2 extends HttpServlet {
    /*
       代码未写完
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String filename = request.getParameter("filename");

        String realPath = this.getServletContext().getRealPath("download"+filename);

        String aent = request.getHeader("User-Agent");
        response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(filename,"utf-8"));
        FileInputStream fis = new FileInputStream(realPath);

        ServletOutputStream out = response.getOutputStream();
        int len;
        byte[] buffer = new byte[1024*8];
        while((len=fis.read(buffer))!=-1){
            out.write(buffer,0,len);
        }

    }

}
